import json
from apps.merchant.item.dao import ItemDao
from apps.user.dao import UserDao
from apps.pay.recordIncomes.dao import RecordIncomesDao
from decimal import Decimal

def expenseSendIncomesActivityHandle(order):
    _order = order
    activityParamsObj = json.loads(_order.activityParams)
    print('activityParamsObj', activityParamsObj)
    
    # 赠送锁定积分
    payway = 'incomesLocked'
    selfSendIncomesLockedRate = Decimal(activityParamsObj.get('selfSendIncomesLockedRate')).quantize(Decimal("0.00"))
    sendIncomes = _order.sellingPrice * selfSendIncomesLockedRate
    UserDao().addWallet(payway, _order.user, sendIncomes)
    data = {}
    data['scene'] = 'sendForExpense'
    data['user'] = _order.user.id
    data['fromUser'] = _order.user.id
    data['orderId'] = _order.orderId
    data['money'] = sendIncomes.quantize(Decimal("0.00"))
    data['moneyLocked'] = sendIncomes.quantize(Decimal("0.00"))
    data['payway'] = payway
    data['changeWay'] = 'add'
    data['orderStatus'] = 'FINISH'
    RecordIncomesDao().create(data)
    # 赠送上级锁定积分
    payway = 'incomesLocked'
    parentSendIncomesLockedRate = Decimal(activityParamsObj.get('parentSendIncomesLockedRate')).quantize(Decimal("0.00"))
    sendIncomes = _order.sellingPrice * parentSendIncomesLockedRate
    if _order.user.parent:
        UserDao().addWallet(payway, _order.user.parent, sendIncomes)
        data = {}
        data['scene'] = 'sendForDirect'
        data['user'] = _order.user.parent.id
        data['fromUser'] = _order.user.id
        data['orderId'] = _order.orderId
        data['money'] = sendIncomes.quantize(Decimal("0.00"))
        data['moneyLocked'] = sendIncomes.quantize(Decimal("0.00"))
        data['payway'] = payway
        data['changeWay'] = 'add'
        data['orderStatus'] = 'FINISH'
        RecordIncomesDao().create(data)

    # 赠送贡献点
    payway = 'contributionValue'
    sendContributionValue = int(_order.sellingPrice / 100)
    UserDao().addWallet(payway, _order.user, sendContributionValue)
    data = {}
    data['scene'] = 'sendForExpense'
    data['user'] = _order.user.id
    data['fromUser'] = _order.user.id
    data['orderId'] = _order.orderId
    data['money'] = sendContributionValue
    data['payway'] = payway
    data['changeWay'] = 'add'
    data['orderStatus'] = 'FINISH'
    RecordIncomesDao().create(data)
    # 赠送上级贡献点
    payway = 'contributionValue'
    parentSendContributionValueRate = Decimal(activityParamsObj.get('parentSendContributionValueRate')).quantize(Decimal("0.00"))
    sendContributionValue = round(sendContributionValue * parentSendContributionValueRate)
    if _order.user.parent:
        UserDao().addWallet(payway, _order.user.parent, sendContributionValue)
        data = {}
        data['scene'] = 'sendForDirect'
        data['user'] = _order.user.parent.id
        data['fromUser'] = _order.user.id
        data['orderId'] = _order.orderId
        data['money'] = sendContributionValue
        data['payway'] = payway
        data['changeWay'] = 'add'
        data['orderStatus'] = 'FINISH'
        RecordIncomesDao().create(data)


    # content = json.loads(order.content)
    # orderItemList = content.get('orderItemList')
    # print('content', content)
    # if len(orderItemList) > 0:
    #     item = ItemDao().findOneByObjId(orderItemList[0].get('item'))
    #     _payScene = 'incomes'
    #     # 计算赠送多少钱
    #     sendIncomes = 0
    #     if item.expenseSendIncomesType == 1:  # 百分比
    #         sendIncomes = _order.realPay * item.expenseSendIncomes * 1
    #     elif item.expenseSendIncomesType == 2:  # 固定数量
    #         sendIncomes = item.expenseSendIncomes
    #     UserDao().addWallet(_payScene, _order.user, sendIncomes)
    #     data = {}
    #     data['scene'] = 'sendForExpense'
    #     data['user'] = _order.user.id
    #     data['fromUser'] = _order.user.id
    #     data['orderId'] = _order.orderId
    #     data['money'] = sendIncomes.quantize(Decimal("0.00"))
    #     data['payway'] = _payScene
    #     data['changeWay'] = 'add'
    #     data['orderStatus'] = 'FINISH'
    #     RecordIncomesDao().create(data)
